$.ajaxSetup({
    async : false
});

//分页条
function demoPage(begin,select,end) {
    document.writeln("<div class=\'p-page text-center\'>");
    document.writeln("                    <nav aria-label=\'Page navigation\'>");
    document.writeln("                        <ul class=\'pagination\'>");
    document.writeln("                            <li>");
    document.writeln("                                <a href=\'#\' aria-label=\'Previous\'>");
    document.writeln("                                    <span aria-hidden=\'true\'>&laquo;</span>");
    document.writeln("                                </a>");
    document.writeln("                            </li>");
    document.writeln("                            <li class='active'><a href=\'#\'>1</a></li>");
    document.writeln("                            <li><a href=\'#\'>2</a></li>");
    document.writeln("                            <li><a href=\'#\'>3</a></li>");
    document.writeln("                            <li><a href=\'#\'>4</a></li>");
    document.writeln("                            <li><a href=\'#\'>5</a></li>");
    document.writeln("                            <li>");
    document.writeln("                                <a href=\'#\' aria-label=\'Next\'>");
    document.writeln("                                    <span aria-hidden=\'true\'>&raquo;</span>");
    document.writeln("                                </a>");
    document.writeln("                            </li>");
    document.writeln("                        </ul>");
    document.writeln("                    </nav>");
    document.writeln("                </div>");
}

//选择型分页条
function page(page,number,parent) {
    //生成最大最小页码
    var maxPage=parseInt(page)+3;
    var minPage=parseInt(page)-3;
    if(minPage<1){
        maxPage+=1-minPage;
        minPage=1;
    }

    if(maxPage>Math.ceil(number/10)){
        minPage-=maxPage-Math.ceil(number/10);
        maxPage=Math.ceil(number/10);
        if(minPage<1){
            minPage=1;
        }
    }

    var html="   <nav aria-label='Page navigation'>\n" +
        "                    <ul class='pagination'>\n" +
        "                        <li>\n" +
        "                            <a aria-label='Previous'>\n" +
        "                                <span aria-hidden='true'>&laquo;</span>\n" +
        "                            </a>\n" +
        "                        </li>";

    for(var i=minPage;i<=maxPage;i++){
        if(i==page){
            html+="<li class='active'><a>"+i+"</a></li>";
        }else {
            html+="<li><a>"+i+"</a></li>";
        }
    }

    html+="<li>\n" +
        "                            <a aria-label='Next'>\n" +
        "                                <span aria-hidden='true'>&raquo;</span>\n" +
        "                            </a>\n" +
        "                        </li>\n" +
        "                    </ul>\n" +
        "                </nav>";

    parent.html(html);
}

//用于控制一些组件的删除操作
function alDelete(event) {
    var alDelete=document.getElementById("chkboxAllowDelete");
    var delButton=document.getElementById("delButton");
    if(alDelete.checked==true){
        delButton.className="btn btn-danger";
        $(delButton).attr("data-dismiss","modal");
        $(delButton).attr("onclick",event);
    }else {
        delButton.className="btn btn-danger disabled";
    }
}

//用于控制左侧导航栏的闭合状态
function navigationOpen(id) {
    var panel=document.getElementById(id);
    panel.className="panel-collapse collapse in";
    panel.style=null;
}

function sureSubmit(message, id) {
    var choice=confirm(message);
    var form=document.getElementById(id);
    if(choice===true){
        form.submit();
    }else {
        return;
    }
}

//选择某个班
function selectClass(className) {
    document.writeln("<div class=\'entry\'>");
    document.writeln("                    <div class=\'btn-group\'>");
    document.writeln("                        <button type=\'button\' class=\'btn btn-default dropdown-toggle\' data-toggle=\'dropdown\' aria-haspopup=\'true\' aria-expanded=\'false\'>");
    document.writeln("                            <span class=\'select\'>"+className+"</span> <span class=\'caret\'></span>");
    document.writeln("                        </button>");
    document.writeln("                        <ul class=\'dropdown-menu\'>");
    document.writeln("                            <li><a href=\'#\'>信息工程一班</a></li>");
    document.writeln("                            <li><a href=\'#\'>信息工程二班</a></li>");
    document.writeln("                            <li><a href=\'#\'>信息工程三班</a></li>");
    document.writeln("                            <li><a href=\'#\'>信息工程四班</a></li>");
    document.writeln("                            <li role=\'separator\' class=\'divider\'></li>");
    document.writeln("                            <li><a href=\'#\'>文学一班</a></li>");
    document.writeln("                            <li><a href=\'#\'>文学二班</a></li>");
    document.writeln("                            <li><a href=\'#\'>文学三班</a></li>");
    document.writeln("                        </ul>");
    document.writeln("                    </div>");
    document.writeln("                </div>");
}

//单选表单
function selectForm(select,choices,def) {
    document.writeln("<div class=\'set-select\'>");
    document.writeln("<select class=\'form-control\'>");
    for(var i=0;i<choices.length;i++){
        if(i===select){
            document.writeln("<option selected>"+choices[i]+"</option>");
        }else {
            document.writeln("<option>"+choices[i]+"</option>");
        }
    }
    document.writeln("</select>");
    document.writeln("</div>");
}

//多选表单
function selectMany(selects, choices) {
    document.writeln("<div class=\'set-select\'>");
    document.writeln("<select class=\'selectpicker\' multiple>");
    for(var i=0,j=0;i<choices.length&&j<selects.length;i++){
        if(i===selects[j]){
            j++;
            document.writeln("<option selected>"+choices[i]+"</option>");
        }else {
            document.writeln("<option>"+choices[i]+"</option>");
        }
    }
    document.writeln("</select>");
    document.writeln("</div>");
}

//获取url参数
function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]); return null;
}

//验证文本输入数据是否完整
function testInputText(parent) {
    var z=0;
    parent.find("input[type='text']").each(function () {
        if(this.value===null||this.value===""){
            alert("存在未填写的输入文本框，请检查");
            z=1;
            return false;
        }
    });

    if(z===1){
        return false;
    }

    parent.find("input[type='number']").each(function () {
        if(this.value===null||this.value===""){
            alert("存在未填写的数字文本框，请检查");
            z=1;
            return false;
        }
    });

    if(z===1){
        return false;
    }

    return true;
}

//刷新题目表格
function flushTable(url,json,pageEle,bodyEle) {
    json=JSON.parse(json);
    var pageIndex=json.page;
    console.log(json);
    $.get(url,json,function (data) {
        console.log(JSON.parse(data));
        //获取data并清空table
        data=JSON.parse(data).data;
        var tbody=$(bodyEle);
        tbody.empty();

        //重做导航栏
        page(json.page,data.number,$(pageEle));
        var options=$(".pagination li");
        json.page=1;
        options.get(0).setAttribute("onclick","flushTable('"+url+"','"+JSON.stringify(json)+"','"+pageEle+"','"+bodyEle+"')");
        for(var i=1;i<options.length-1;i++){
            json.page=options.get(i).getElementsByTagName("a")[0].innerHTML;
            options.get(i).setAttribute("onclick","flushTable('"+url+"','"+JSON.stringify(json)+"','"+pageEle+"','"+bodyEle+"')");
        }
        json.page=Math.ceil(data.number/10);

        options.get(options.length-1).setAttribute("onclick","flushTable('"+url+"','"+JSON.stringify(json)+"','"+pageEle+"','"+bodyEle+"')");

        //给table新增相关的项
        data=data.data;
        for(var i=0;i<data.length;i++){
            addLine(data[i],url,parseInt(pageIndex),i);
        }
    });
}

//问题类型对应的html
var questionType=["<td style='color: green'>选择题</td>",
    "<td style='color: orange'>判断题</td>",
    "<td style='color: black'>填空题</td>",
    "<td style='color: #7a43b6'>阅读题</td>",
    "<td style='color: #7a43b6'>阅读题内小题</td>"];

//难度类型对应的html
var questionLevel=["<td style='color: green'>易</td>",
    "<td style='color: orange'>中</td>",
    "<td style='color: red'>难</td>",
    "<td style='color: black'>无</td>"];

//根据返回的状态输出提示信息
function message(data) {
    data=JSON.parse(data);
    if (data.code==0){
        alert("操作成功");
    }else {
        alert(data.msg);
    }
}